/* $RCSFile: ChangeURLRule.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.RewriteRules/ChangeURLRule.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Globalization;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Text.RegularExpressions;

using Rewrite.NET;

namespace AddonNice.RewriteRules
{
	/// <summary>
    /// ChangeURLRule
    /// CS Class used to change urls with a regex rule, can't act on host names
    /// </summary>
	/// </summary>
	public class ChangeURLRule: IRules
	{
		public	bool BreakAfter
		{
			get
			{
				return Section.BreakOnAfter;
			}
            set {}
	    }

		public	bool BreakOnFirst
		{
			get
			{
				return Section.BreakOnFirst;
			}
            set {}
	    }

		IndexRule _Section	=	null;
		public IndexRule Section
		{
			get
			{
				return _Section;
			}
			set
			{
				_Section	=	value;
			}
		}
      
		public bool Execute(HttpApplication Appl,ref string Path)
        {
		    if ( RewriteTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ChangeUrlRule Execute BEG BreakOnFirst: {0}, BreakAfter: {1}, Path: '{2}'",BreakOnFirst,BreakAfter,Path),RewriteTraceSwitch.Sw.Info);
            bool changed            =   false;
            foreach(RuleParameter rp in Section.Parameters)
            {
                string ob1          =   rp.SearchExpression;
			    if ( string.IsNullOrEmpty(ob1) )
				    continue;
			    string ob2			=	rp.Value;
			    if ( RewriteTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ChangeUrlRule Execute URL2Change: '{0}', URLChanged: '{1}', Path: '{2}'", ob1,ob2,Path),RewriteTraceSwitch.Sw.Info);
                if ( Substitution(ob1,ref Path,ob2)  )
                    changed =   true;
                if ( changed && BreakOnFirst )
                    break;
            }
	        if ( RewriteTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ChangeUrlRule Execute END Changed: {0}, result: '{1}'",changed,Path),RewriteTraceSwitch.Sw.Info);
			return changed; //no change
        }


		bool Substitution(string strReg1,ref string zPath,string replace) 
        {
			Regex oReg      =   new Regex(strReg1);
		    Match oMatch    =   oReg.Match(zPath);
		    if(oMatch.Success) 
					zPath   =   oReg.Replace(zPath,replace);
	        if ( RewriteTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ChangeUrlRule Substitution Regex: '{0}', Match: {1}, Path: '{2}'.",strReg1,oMatch.Success,zPath),RewriteTraceSwitch.Sw.Info);
			return oMatch.Success;
		}
    }

}

